package com.wy.android.mp.infoquery;


import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.maps.MyLocationOverlay;
import com.wy.android.mp.R;
import com.wy.android.util.HttpUtil;
public class EscapedQueryActivity extends Activity {
	
	private Button button1,button2,button3;
	private EditText editText;    //身份证
	private TextView textView;    //查询结果
    private ImageView imageView;   //查询的照片
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.escape_people_query);
		
		button1=(Button) findViewById(R.id.button1);
		button2=(Button) findViewById(R.id.button2);
		editText=(EditText) findViewById(R.id.editText1);
		textView=(TextView) findViewById(R.id.resultTextView);
		imageView=(ImageView) findViewById(R.id.imageView1);
		
		button3.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		//查询信息
		button1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(validate()){
					String idno=editText.getText().toString();
					//获得查询信息
					String msg=query(idno);
					if(msg!=null&&!msg.equals("")){
						textView.setText(msg);
					}else{
						textView.setText("无此人的信息！");
					}		
				}
			
			}
		});
		
		//查询照片
		button2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(validate()){
					String idno=editText.getText().toString();
					//获得照片路径
					String path=queryPicPath(idno);;
					String pathz=HttpUtil.BASE_URL+path;
				//	String path="http://10.0.2.2:8088/zfkchaper_Moblie_PoliceServer/images/android-wy.jpg";
					if(pathz!=null&&!pathz.equals("")){
						showPic(pathz);
					}	
				}
			}
		});
		
		
	}
	
	//显示对话框
	private void showDialog(){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("请输入身份证号！")
		       .setCancelable(false)
		       .setPositiveButton("确定", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		           }
		       });
		AlertDialog alert = builder.create();
		alert.show();
	}
	
	
	//验证非空
	private boolean validate(){
		String idno = editText.getText().toString();
		if(idno.equals("")||idno==null){
			showDialog();
			return false;
		}
		return true;
	}
	
	//查询方法,并返回响应结果
	private String query(String idno){
		String queryString = "idno="+idno;
		String url = HttpUtil.BASE_URL+"servlet/PeopleServlet?"+queryString;
		return HttpUtil.queryStringForPost(url);
    }
	
	//查询方法,照片路径
	private String queryPicPath(String idno){
		String queryString = "idno="+idno;
		String url = HttpUtil.BASE_URL+"servlet/PeopleImgServlet?"+queryString;
		return HttpUtil.queryStringForPost(url);
    }
	
	//通过url，查询照片方法
	private void showPic(String urlStr){
		
		try {
			//将urlstr转换成url对象
			URL url=new URL(urlStr);
		    URLConnection conn=url.openConnection();
		    conn.connect();
		    InputStream in=conn.getInputStream();
		    
		    Bitmap bitmap=BitmapFactory.decodeStream(in);
		    
		    imageView.setImageBitmap(bitmap);		    
		}catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			
		}
		
		
		
	}

}
